package com.rcd2.web.servlet;

import com.alibaba.fastjson.JSON;
import com.rcd2.pojo.Brand;
import com.rcd2.pojo.PageBean;
import com.rcd2.service.BrandService;
import com.rcd2.service.impl.BrandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

/**
 * ClassName:${NAME}
 * Package:${PACKAGE_NAME}
 * ProjectName:servlet
 * Description:
 *
 * @Date:2022/2/9 23:21
 * @Author:Mrhan
 */

@WebServlet("/brand/*")
public class BrandServlet extends BaseServlet {
    private BrandService brandService = new BrandServiceImpl();

    public void selectAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 调用service查询
        List<Brand> brands = brandService.selectAll();

        //2. 转为JSON
        String jsonString = JSON.toJSONString(brands);
        //3. 写数据
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        //1. 接收品牌数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串

        //转为Brand对象
        Brand brand = JSON.parseObject(params, Brand.class);

        //2. 调用service添加
        brandService.add(brand);

        //3. 响应成功的标识
        response.getWriter().write("success");
    }

    public void deleteByIds(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        //1. 接收品牌数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串

        /**
         * 因为接收到前端的参数是[id1,id2,id3],
         * 所以,我们要json转数组对象*/
        int[] ids = JSON.parseObject(params, int[].class);

        //2. 调用service添加
        brandService.deleteByIds(ids);

        //3. 响应成功的标识
        response.getWriter().write("success");
    }

    public void selectByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        /**
         * _currentPage和_pageSize代表临时变量
         * */
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.valueOf(_currentPage);
        int pageSize = Integer.valueOf(_pageSize);

        PageBean<Brand> pageBean = brandService.selectByPage(currentPage, pageSize);

        String jsonString = JSON.toJSONString(pageBean);

        //3. 响应成功的标识
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void selectByPageAndCondition(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //处理URL里中文乱码的问题
        request.setCharacterEncoding("utf-8");
        //_currentPage和_pageSize代表临时变量
        //这里获取URL上拼接的参数(和get请求获取参数一样)
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");

        int currentPage = Integer.valueOf(_currentPage);
        int pageSize = Integer.valueOf(_pageSize);

        //获取post请求的参数
        BufferedReader reader = request.getReader();
        String params = reader.readLine();
        Brand brand = JSON.parseObject(params, Brand.class);

        PageBean<Brand> pageBean = brandService.selectByPageAndCondition(currentPage, pageSize, brand);
        String jsonString = JSON.toJSONString(pageBean);

        //3. 响应成功的标识
        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }


}
